<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="query.keyword" placeholder="请输入标签名进行搜索" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves style="margin-left: 10px;" size="medium" class="filter-item" type="primary"
                 icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      style="margin-top: 20px;"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="物品名称" align="center">
        <template slot-scope="{row}">
          {{ row.thing_name }}
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" width="100">
        <template slot-scope="{row}">
          {{ row.User.name }}
        </template>
      </el-table-column>
      <el-table-column label="审批状态" class-name="status-col" align="center" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.approval_status | statusFilter">
            {{ row.approval_status | getStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.createAt | dayFormatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" width="200">
        <template slot-scope="{row}">
          {{ row.updateAt | dayFormatTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="small" @click="handleUpdate(row)">审批</el-button>
          <el-button type="danger" size="small" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="text-align: right"
      v-show="total>0"
      :total="total"
      :page.sync="query.offset"
      :limit.sync="query.limit"
      @pagination="getList"
    />

    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">
      <el-form ref="dataForm" :model="temp" label-width="80px" label-position="left">
        <el-form-item label="申请人">
          <span>{{ temp.User.name }}</span>
        </el-form-item>
        <el-form-item label="物品名称">
          <span>{{ temp.thing_name }}</span>
        </el-form-item>
        <el-form-item label="物品类型">
          <el-select @change="typeChange" v-model="labelValue" multiple placeholder="请选择">
            <el-option
              v-for="item in labelList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品图片">
          <template v-for="item in temp.images">
            <el-image :src="item | getImage" :preview-src-list="temp.images | getImages"
                      style="width: 150px;margin: 5px"></el-image>
          </template>
        </el-form-item>
        <el-form-item label="物品描述">
          <span>{{ temp.thing_describe }}</span>
        </el-form-item>
        <el-form-item label="鉴定图片">
          <template v-for="item in temp.certificateImages">
            <el-image :src="item | getImage" :preview-src-list="temp.certificateImages | getImages"
                      style="width: 150px;margin: 5px"></el-image>
          </template>
        </el-form-item>
        <!--        <el-form-item label="物品视频">-->
        <!--          <video :src="temp.thing_video" controls="controls"></video>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="reject">
          驳回
        </el-button>
        <el-button type="primary" @click="pass">
          通过
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, create, fetchDetail, remove, update, changeStatus, changeTypes } from '@/api/apply'
import { fetchList as fetchLabel } from '@/api/label'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { expertType } from '@/api/user' // waves directive

export default {
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'gray',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    },
    getImages(images = []) {
      return images.map(it => {
        return process.env.VUE_APP_BASE_API + '/' + it
      })
    },
    getStatus(status) {
      const arr = ['审核中', '已通过', '已驳回', '评测完成']
      return arr[status]
    }
  },
  data() {
    return {
      total: 0,
      query: {
        limit: 10,
        offset: 1,
        keyword: ''
      },
      textMap: {
        update: '详情',
        create: '新增'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      list: null,
      listLoading: true,
      labelList: [], // select选项
      labelValue: [], // select选中的值
      temp: {
        id: '',
        thing_name: '',
        thing_images: '',
        thing_video: '',
        thing_describe: '',
        certificate: '',
        User: {
          name: ''
        }
      }
    }
  },
  created() {
    this.getList()
    this.getLabelList()
  },
  methods: {
    handleFilter() {
      this.query.offset = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList(this.query).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 获取标签列表
    getLabelList() {
      fetchLabel({
        limit: 1000,
        offset: 1,
        keyword: ''
      }).then(res => {
        this.labelList = res.data.list
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.images = this.temp.thing_images.split(',')
      this.temp.certificateImages = this.temp.certificate.split(',')
      // 将'['1','2','3']'变成number数组
      if (this.labelValue) {
        this.labelValue = this.temp.types?.replace(/\[|]/g, '').split(',').map(Number)
      } else {
        this.labelValue = []
      }
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      if (this.$refs['dataForm']) {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    // select值改变时保存类型
    typeChange() {
      changeTypes({ id: this.temp.id, type: this.labelValue.toString() }).then(res => {
        this.getList()
      })
    },
    // 通过
    pass() {
      changeStatus({ id: this.temp.id, status: 1 }).then(res => {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 1000
        })
        this.dialogFormVisible = false
        this.getList()
      })
    },
    // 驳回
    reject() {
      changeStatus({ id: this.temp.id, status: 2 }).then(res => {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 1000
        })
        this.dialogFormVisible = false
        this.getList()
      })
    },
    handleDelete(row, index) {
      this.$confirm('确定删除吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          remove(row.id).then(res => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1000
            })
            this.getList()
          })
        })
        .catch(err => {
          console.error(err)
        })
    }
  }
}
</script>
